<?php
/*
 * TODO: make it something useful 
 *
 * Factory for access objects
 * 
 * Most objects are for accessing database transparently
 */

m2import('dba/DataSource.php');
 
class base_AccessFactory
{
    
    var $conn;
    var $cache;

    static public $CMS = 'CMS';
    static public $CMSLang = 'CMSLang';
    static public $CMSDeploy = 'CMSDeploy';
    static public $CMSImage = 'CMSImage';
    static public $CMSObject = 'CMSObject';
    static public $CMSUserComment = 'CMSUserComment';
    static public $CSS = 'CSS';
    static public $Customer = 'Customer';
    static public $MwSt = 'MwSt';
    static public $Newsletter = 'Newsletter';
    static public $Module = 'Module';
    static public $ObjectTree = 'ObjectTree';
    static public $OnlineUser = 'OnlineUser';
    static public $Order = 'Order';
    static public $Permission = 'Permission';
    static public $Sites = 'Sites';
    static public $User = 'User';
    static public $ImgManTree = 'ImgManTree';

    /**
     * package local
     */
    function base_AccessFactory ()
    {
        $ds = & dba_DataSource::getInstance();
        $this->conn = & $ds->getConnection();
    }

    /**
     *
     * @return udba Connection
     */
    function & getConnection()
    {
        return $this->conn;
    }
    
    /**
     * Get an instance of a data base access class
     * 
     * @param string $key Name of the class (case-sensitive) without 'dba' prefix.
     * @param mixed  $opt1  First optional argument for constructor
     * @param mixed  $opt2  Second optional argument for constructor
     * @param mixed  $opt3  Third optional argument for constructor
     * @param mixed  $opt4  Fourth optional argument for constructor
     */
    function & getInstanceOf($key)
    {
        $numArgs = func_num_args();
        $args = func_get_args();
    	$classname = "dba".$key;
        if (empty($this->cache[$key.'_'.$numArgs]))
        {
        	if (!class_exists($classname))
        		m2import('dba/'.$classname.'.o.inc.php');
        	switch (func_num_args()) 
        	{
        	    case 2:
        	        $this->cache[$key.'_'.$numArgs] = new $classname($this->conn, $args[1]);
        	        break;
        	    case 3:
        	        $this->cache[$key.'_'.$numArgs] = new $classname($this->conn, $args[1],
        	                                                    $args[2]);
        	        break;
        	    case 4:
        	        $this->cache[$key.'_'.$numArgs] = new $classname($this->conn, $args[1],
        	                                                    $args[2], 
        	                                                    $args[3]);
        	        break;
        	    case 5:
        	        $this->cache[$key.'_'.$numArgs] = new $classname($this->conn, $args[1],
        	                                                    $args[2], 
        	                                                    $args[3], 
        	                                                    $args[4]);
        	        break;
        	    default:
        	        $this->cache[$key.'_'.$numArgs] = new $classname($this->conn);
        	}
        }
        return $this->cache[$key.'_'.$numArgs];
    }

    /**
     * Get an instance of a data base access class, force a new instance
     * 
     * @param string $key Name of the class (case-sensitive) without 'dba' prefix.
     * @param mixed  opt  First optional argument for constructor
     * @param mixed  opt  Second optional argument for constructor
     * @param mixed  opt  Third optional argument for constructor
     * @param mixed  opt  Fourth optional argument for constructor
     */
    function & getNewInstanceOf($key)
    {
    	$classname = "dba".$key;
       	if (!class_exists($classname))
       		m2import('dba/'.$classname.'.o.inc.php');
       	$args = func_get_args();
       	switch (func_num_args()) 
       	{
       	    case 2:
       	        $this->cache[$key] = new $classname($this->conn, $args[1]);
       	        break;
       	    case 3:
       	        $this->cache[$key] = new $classname($this->conn, $args[1], 
       	                                                    $args[2]);
       	        break;
       	    case 4:
       	        $this->cache[$key] = new $classname($this->conn, $args[1],
       	                                                    $args[2], 
       	                                                    $args[3]);
       	        break;
       	    case 5:
       	        $this->cache[$key] = new $classname($this->conn, $args[1],
       	                                                    $args[2], 
       	                                                    $args[3], 
       	                                                    $args[4]);
       	        break;
       	    default:
       	        $this->cache[$key] = new $classname($this->conn);
       	}
       	return $this->cache[$key];
    }

}

?>
